package cn.jja8.bukkit.bukkitcommandtool.command;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.io.IOException;

public class CommandManger implements CanSetUp{
    Command command;
    public CommandManger(Command command) {
        this.command = command;
    }
    /**
     * 加载命令配置文件
     * */
    @Override
    public void load(ConfigurationSectionSetDefGet configurationSectionSetDefGet) {
        command =  configurationSectionSetDefGet.getObjectSetDef(command.getMainCommand(),command);
    }

    /**
     * 加载命令配置文件
     * */
    public CommandManger load(ConfigurationSection configurationSection) {
        ConfigurationSectionSetDefGet configurationSectionSetDefGet = new ConfigurationSectionSetDefGet(configurationSection);
        load(configurationSectionSetDefGet);
        return this;
    }
    /**
     * 加载命令配置文件
     * */
    public CommandManger load(File file) {
        YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
        ConfigurationSectionSetDefGet configurationSectionSetDefGet = new ConfigurationSectionSetDefGet(yamlConfiguration);
        load(configurationSectionSetDefGet);
        try {
            yamlConfiguration.save(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 把这个命令加载到服务器中
     * */
    public CommandMangering run(Plugin main){
        if (command instanceof NeedFirst){
            ((NeedFirst)command).first();
        }
        return new CommandMangering(main,command);
    }

}
